home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 12984 / 12984.xpi / chrome / VideoDownloaderToolbar.jar / content / docanalyzer.js < prev    next >
Text File  |  2010-01-29  |  5KB  |  191 lines

  1. if(!com) var com={};
  2. if(!com.VidBar) com.VidBar={};
  3.  
  4. com.VidBar.DocUtil = {
  5.     xpGetSingleNode : function(node, xpath) {
  6.         var anode = node.ownerDocument.evaluate(xpath, node, null,
  7.                 XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  8.         return anode;
  9.     },
  10.     xpGetNodes : function(node, xpath) {
  11.         var nodes = [];
  12.         var xpr = node.ownerDocument.evaluate(xpath, node, null,
  13.                 XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
  14.         var node0 = xpr.iterateNext();
  15.         while (node0 != null) {
  16.             nodes.push(node0);
  17.             node0 = xpr.iterateNext();
  18.         }
  19.         return nodes;
  20.     },
  21.     xpGetStrings : function(node, xpath) {
  22.         var strings = [];
  23.         var xpr = node.ownerDocument.evaluate(xpath, node, null,
  24.                 XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
  25.         var node0 = xpr.iterateNext();
  26.         while (node0 != null) {
  27.             if (node0.nodeType == Node.TEXT_NODE)
  28.                 strings.push(node0.nodeValue);
  29.             else if (node0.firstChild != null
  30.                     && node0.firstChild.nodeType == Node.TEXT_NODE)
  31.                 strings.push(node0.firstChild.nodeValue);
  32.             node0 = xpr.iterateNext();
  33.         }
  34.         return strings;
  35.     },
  36.     xpGetString : function(node, xpath) {
  37.         var text = node.ownerDocument.evaluate(xpath, node, null,
  38.                 XPathResult.STRING_TYPE, null).stringValue;
  39.         return text;
  40.     }
  41. }
  42.  
  43. com.VidBar.LinkAnalyzer = function(pref) {
  44.     this.pref = pref;
  45. }
  46.  
  47. com.VidBar.LinkAnalyzer.prototype = {
  48.     name : "LinkAnalyzer",
  49.     analyzeDoc : function(doc) {
  50.         // com.VidBar.__d("com.VidBar.LinkAnalyzer.analyzeDoc");
  51.         var list = [];
  52.  
  53.         try {
  54.             if (doc == null)
  55.                 doc = content.document;
  56.  
  57.             var mediaPattern = this.pref.getMediaExtensions();
  58.             var pat = new RegExp("^.*\\.(?:" + mediaPattern + ")$", "i");
  59.  
  60.             var baseUrl = Components.classes["@mozilla.org/network/standard-url;1"]
  61.                     .createInstance(Components.interfaces.nsIURI);
  62.             baseUrl.spec = doc.URL;
  63.  
  64.             var dom = doc.documentElement;
  65.             var allHRefs = {};
  66.             var linkNodes = com.VidBar.DocUtil.xpGetNodes(dom, ".//a[@href]", {});
  67.             for (var i = 0; i < linkNodes.length; i++) {
  68.                 try {
  69.                     var node = linkNodes[i];
  70.  
  71.                     var href = node.getAttribute("href");
  72.                     if (allHRefs[href] != null)
  73.                         continue;
  74.                     allHRefs[href] = "";
  75.  
  76.                     if (pat.exec(href) == null)
  77.                         continue;
  78.  
  79.                     var hrefParts = /^(.*[^0-9])?([0-9]+)([^\/]*?\.[^\/]*?)$/
  80.                             .exec(href);
  81.                     var extension = /.*\.(.*?)$/.exec(hrefParts[3])[1];
  82.  
  83.                     var mediaUrl = baseUrl.resolve(href);
  84.  
  85.                     var filename = /.*\/(.*?)$/.exec(mediaUrl)[1];
  86.                     var label = filename;
  87.                     try {
  88.                         var ih = node.innerHTML;
  89.                         label = ih.split(/<\/?[^>]+>/).join("");
  90.                     } catch (e) {
  91.                     }
  92.                     if (label == "")
  93.                         label = filename;
  94.  
  95.                     var data = {
  96.                         type : "link",
  97.                         label : label,
  98.                         filename : filename,
  99.                         extension : extension,
  100.                         pageUrl : doc.URL,
  101.                         mediaUrl : mediaUrl,
  102.                         referrer : doc.URL
  103.                     };
  104.                     list.push(data);
  105.                 } catch (e) {
  106.                     com.VidBar.__e("com.VidBar.LinkAnalyzer.analyzeDoc: " + e);
  107.                 }
  108.             }
  109.  
  110.         } catch (e) {
  111.             com.VidBar.__e("com.VidBar.LinkAnalyzer.analyzeDoc: " + e);
  112.         }
  113.  
  114.         return list;
  115.     }
  116. }
  117.  
  118. com.VidBar.YouTubeAnalyzer = function(pref) {
  119.     this.pref = pref;
  120. }
  121.  
  122. com.VidBar.YouTubeAnalyzer.prototype = {
  123.     name : "YoutubeAnalyzer",
  124.     analyzeDoc : function(doc) {
  125.         // com.VidBar.__d("com.VidBar.YouTubeAnalyzer.analyzeDoc");
  126.         var list = [];
  127.         try {
  128.             if (/^http:\/\/[^\/]*\.?youtube\.[^\/\.]+/.test(doc.URL)) {
  129.                 var dom = doc.documentElement;
  130.                 var scripts = com.VidBar.DocUtil.xpGetStrings(dom, ".//script/text()", {});
  131.                 var videoId = null;
  132.                 var t = null;
  133.                 for (var i = 0; i < scripts.length; i++) {
  134.                     var script = scripts[i];
  135.                     var match = /video_id=([^&]+)(?:&.*)&t=([^&]+)/m
  136.                             .exec(script);
  137.                     if (match != null && match.length == 3) {
  138.                         videoId = match[1];
  139.                         t = match[2];
  140.                         break;
  141.                     }
  142.                     var match = /[&\?]t=(.*?)&.*video_id=(.*?)(?:&|")/m
  143.                             .exec(script);
  144.                     if (match != null && match.length == 3) {
  145.                         videoId = match[2];
  146.                         t = match[1];
  147.                         break;
  148.                     }
  149.                 }
  150.                 if (videoId == null || t == null) {
  151.                     var embeds = com.VidBar.DocUtil.xpGetStrings(dom, ".//embed/@src", {});
  152.                     for (var i = 0; i < embeds.length; i++) {
  153.                         var embed = embeds[i];
  154.                         var match = /video_id=(.*?)&.*t=(.*?)(?:&|")/m
  155.                                 .exec(embed);
  156.                         if (match != null && match.length == 3) {
  157.                             videoId = match[1];
  158.                             t = match[2];
  159.                             break;
  160.                         }
  161.                     }
  162.                     if (videoId == null || t == null) {
  163.                         return;
  164.                     }
  165.                 }
  166.                 var title = com.VidBar.DocUtil.xpGetString(dom,
  167.                         "/html/head/meta[@name='title']/@content");
  168.                 var url = "http://www.youtube.com/get_video?video_id="
  169.                         + videoId + "&t=" + t;
  170.  
  171.                 var fileName = title;
  172.                 fileName = fileName.replace(/[^a-zA-Z0-9\.\-]/g, "_");
  173.  
  174.                 var data = {
  175.                     type : "youtube",
  176.                     label : title,
  177.                     filename : fileName + ".flv",
  178.                     extension : "flv",
  179.                     pageUrl : doc.URL,
  180.                     mediaUrl : url,
  181.                     referrer : doc.URL
  182.                 };
  183.                 list.push(data);
  184.             }
  185.         } catch (e) {
  186.             com.VidBar.__e("com.VidBar.YouTubeAnalyzer.analyzeDoc error: " + e);
  187.         }
  188.         return list;
  189.     }
  190. };
  191.